import React ,{useState,useReducer} from 'react'

function App () {
    // 用usereducer状态管理钩子   redux
    // action = {
    //     type:'add'
    // }
    const reducer = (state,action) =>{
        const actionFn = {
            add:function() {
                return {
                    ...state,
                    count:state.count+1
                }
            }
        }

        return actionFn[action.type]()
    }

    const [state,dispatch] = useReducer(reducer,{count:0})

    // function useReducer(a,b) {
    //     // 复杂的处理 
    //     return [b,dispatch=(obj){
    //         // 内部关联了a 
    //         a(state,obj)
    //     }]
    // }

    // addcount 点击+1处理函数 
    const addcount=()=>{
        dispatch({
            type:'add'
        })
    }
      
   return (
       <div>
           <div>{state.count}</div>
           <button onClick={addcount}>点击+1</button>
       </div>
   ) 

}


export default App